<template>
  <div class="weather-warp">

    <div class="content">
      <div v-for="(item, index) in jsonpData" :key="index" class="weather-item" @click="handleWeather(item)">
        <span class="address">{{ item.adm1 }}-{{ item.adm2 }}/{{ item.name }}</span>
        <span class="time">{{ item.utcOffset }}</span>
        <div class="weather-number">
          <div class="icon-wrap">
            <svg class="icon" aria-hidden="true">
              <use :xlink:href="`#icon-${item.icons.path}`"></use>
            </svg>
            <span class="icon-name">{{item.icons.name}}</span>
          </div>
         
          <span class="number">{{item.rank}}℃</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import {
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
} from "vue";
import { weatherIcon } from "../common/assetsConst";
import { iosFormatDate } from "../utils/module";
// import Cascader from "../components/cascader.vue";
import axios from "axios";
const weather = ref("");
let jsonpData = ref([]);

const loadJsonpData = () => {
  let url =
    "https://geoapi.heweather.net/v2/city/lookup?location=beijing&key=a38457c8d0b24333aa2262595ebb55b6";
  axios({
    method: "get",
    url: url,
    withCredentials: false,
  }).then(function (res) {
    if (res.data.code === "200") {
      // 获取当前日期和时间
      const formatDate= iosFormatDate()
      res.data.location.forEach((item,index) => {
        // 随机图标的下标
        const mathIndex = Math.floor(Math.random() * 10)
        // 获取随机的图标
        item.icons =weatherIcon()[mathIndex]
        // 计算10天时间
        let isDay = Number(formatDate.day)+index<10?"0"+(Number(formatDate.day)+index):Number(formatDate.day)+index
        item.utcOffset = formatDate.year+"-"+formatDate.month+"-"+isDay+" "+item.utcOffset.replace('+', "").replace(/GMT/, "")
      });
      jsonpData.value = JSON.parse(JSON.stringify(res.data.location));

      console.log(jsonpData.value);
    }
  });
};
const handleWeather = (item)=>{
  console.log(item)
  window.open(`${item.fxLink}`)

}
onMounted(() => {
  console.log("alalala");
  loadJsonpData();
});

onBeforeUpdate(() => {}),
  onUpdated(() => {}),
  onBeforeUnmount(() => {}),
  onUnmounted(() => {});
</script>
<style scoped lang="scss">
  .weather-warp{
    display: flex;
    justify-content: center;

  }
.content {
  min-width: 1288px;
  width: 65%;
  display: flex;
  flex-wrap: wrap;
  /* align-items: center; */
  justify-content: space-around;

  padding: 20px;
}
.weather-item {
  width: 200px;
  padding:10px;
  border: 1px solid #ccc;
  border-radius: 10px;
  box-shadow: inset 0px 0px 10px #ccc;
  margin-bottom: 20px;
  margin-right: 20px;
  .address,.time{
    display: block;
    width: 100%;
    font-size: 14px;
  }
  .time{
    font-size: 14px;
    margin-top: 10px;
  }
  .weather-number{
    padding: 10px 15px;
    display: flex;
    justify-content: space-between;
    .icon-wrap{
      display: flex;
      align-items: end;
    }
    .icon-name{
      margin-left: 10px;
      margin-bottom: 10px;
    }
    .number{
      font-size: 20px;
    }
  }
}
</style>
<style></style>
